package mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import utils.StringHelper;
import config.Constant;

public class MailSender {
	
	public static String MAIL_SERVER = "www.tele-hot.com";
	public static String MAIL_USER = "sunzhaohui@tele-hot.com";
	public static String MAIL_USER_PWD = "111111";
	
	public static void sendMail(String sendto, String title, String content) {
		try {
			Properties props = new Properties();
			Session sendMailSession;
			String needAuth = "true";
			String mailserver = MAIL_SERVER;
			String mailuser = MAIL_USER;
			String mailuserpass = MAIL_USER_PWD;
			
			if (StringHelper.parseBoolean(needAuth)) {
				PopupAuthenticator popAuthenticator = new PopupAuthenticator();
				popAuthenticator.performCheck(mailuser, mailuserpass);
				props.put("mail.smtp.host", mailserver);
				props.put("mail.smtp.auth", Constant.TRUE);
				sendMailSession = Session.getDefaultInstance(props,
						popAuthenticator);

			} else {
				sendMailSession = Session.getInstance(props, null);
				props.put("mail.smtp.host", mailserver);
			}

			Message newMessage = new MimeMessage(sendMailSession);
			newMessage.setFrom(new InternetAddress("sunzhaohui@tele-hot.com"));
			newMessage.setRecipient(Message.RecipientType.TO,
					new InternetAddress(sendto));
			newMessage.setSubject(title);
			newMessage.setSentDate(new Date());
			newMessage.setContent(content, "text/html;charset=UTF-8");
			
			Transport.send(newMessage);
			
		} catch (MessagingException me) {
			me.printStackTrace();
		}
	}

	public static void main(String[] args) {

		sendMail("sunzhaohui@tele-hot.com", "测试", "测试");
	}
}
